﻿using UnityEngine;
using System.Collections;

[AddComponentMenu("MyGame/Enemy")]
public class Enemy : MonoBehaviour {

    //移动速度
    public float m_speed = 1;
    //旋转速度
    protected float m_rotSpeed = 30;

    protected Transform m_transform;
    //定义一个生命属性
    public float m_life = 1.0f;

    //爆炸特效
    public Transform m_explosionFX;

    //分数
    public int m_point = 10;

	// Use this for initialization
	void Start () {
        m_transform = this.transform;
	}
	
	// Update is called once per frame
	void Update () {
        UpdateMove();
	}

    protected virtual void UpdateMove()
    {
        //左右移动
        float rx = Mathf.Sin(Time.time) * Time.deltaTime;

        //前进
        m_transform.Translate(new Vector3(rx,0,-m_speed*Time.deltaTime));
    }

    //检测碰撞
    void OnTriggerEnter(Collider other)
    {
        if (other.tag.CompareTo("PlayerRocket") == 0)
        {
            Rocket rocket = other.GetComponent<Rocket>();
            if(rocket!=null){
                m_life -= rocket.m_power;
                if(m_life<=0){
                    GameManager.Instance.AddScore(m_point);

                    Instantiate(m_explosionFX, m_transform.position, Quaternion.identity);
                    Destroy(this.gameObject, 0.1f);
                }
            }else if(other.tag.CompareTo("Player")==0){
                m_life = 0;
                Instantiate(m_explosionFX, m_transform.position, m_transform.rotation);
                Destroy(this.gameObject);
            }
        }
    }
}
